package com.southwind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southwind.entity.TbAssertOrg;
import com.southwind.entity.TbAsset;
import com.southwind.entity.TbTaskAttack;
import com.southwind.form.SearchForm;
import com.southwind.mapper.TbAssertOrgMapper;
import com.southwind.mapper.TbIfadminMapper;
import com.southwind.mapper.TbTaskAttackMapper;
import com.southwind.service.TbTaskAttackService;
import com.southwind.util.CommonUtil;
import com.southwind.vo.PageVO;
import com.southwind.vo.TbAssetVO;
import com.southwind.vo.TbTaskAttackVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fjh
 * @since 2024-01-09
 */
@Service
public class TbTaskAttackServiceImpl extends ServiceImpl<TbTaskAttackMapper, TbTaskAttack> implements TbTaskAttackService {
   @Autowired
   private TbTaskAttackMapper tbTaskAttackMapper;
    @Autowired
    private TbAssertOrgMapper tbAssertOrgMapper;
    @Autowired
    private TbIfadminMapper tbIfadminMapper;


    @Override
    public Boolean saveTask(TbTaskAttack tbTaskAttack) {
        tbTaskAttack.setStartTime(CommonUtil.createDate());
        int insert = this.tbTaskAttackMapper.insert(tbTaskAttack);
        return insert == 1;
    }

    @Override
    public PageVO list(Integer page, Integer size) {
        Page<TbTaskAttack> tbTaskAttackPage = new Page<>(page, size);
        Page<TbTaskAttack> resultPage = this.tbTaskAttackMapper.selectPage(tbTaskAttackPage, null);


        List<TbTaskAttackVO> tbTaskAttackVOList = new ArrayList<>();
        for (TbTaskAttack tbTaskAttack : resultPage.getRecords()) {
            TbTaskAttackVO tbTaskAttackVO = new TbTaskAttackVO();
            BeanUtils.copyProperties(tbTaskAttack, tbTaskAttackVO);
//            tbTaskAttackVO.setOrgName(this.tbAssertOrgMapper.selectById(tbTaskAttack.getRelatedOrg()).getOrgName());
//tbTaskAttackVO.setUserName(this.tbIfadminMapper.selectById(tbTaskAttack.getJoinedTesters()).getUsername());
tbTaskAttackVO.setJoinedTesters(this.tbIfadminMapper.selectById(tbTaskAttack.getJoinedTesters()).getUsername());
tbTaskAttackVO.setRelatedOrg(this.tbAssertOrgMapper.selectById(tbTaskAttack.getRelatedOrg()).getOrgName());
// 设置资产名称
            tbTaskAttackVO.setTaskName(tbTaskAttack.getTaskName());
            tbTaskAttackVOList.add(tbTaskAttackVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(tbTaskAttackVOList);
        pageVO.setTotal(resultPage.getTotal());
        return pageVO;
    }


    @Override
    public PageVO search(SearchForm searchForm) {
//        模糊查询+分页
        Page<TbTaskAttack> tbTaskAttackPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<TbTaskAttack> resultPage = null;
        if(searchForm.getValue().equals("")){
            resultPage = this.tbTaskAttackMapper.selectPage(tbTaskAttackPage, null);
        } else {
            QueryWrapper<TbTaskAttack> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            resultPage = this.tbTaskAttackMapper.selectPage(tbTaskAttackPage,queryWrapper);
        }
        List<TbTaskAttackVO> tbTaskAttackVOList = new ArrayList<>();
        for (TbTaskAttack tbTaskAttack : resultPage.getRecords()) {
            TbTaskAttackVO tbTaskAttackVO = new TbTaskAttackVO();
            BeanUtils.copyProperties(tbTaskAttack, tbTaskAttackVO);
//            tbTaskAttackVO.setOrgName(this.tbAssertOrgMapper.selectById(tbTaskAttack.getRelatedOrg()).getOrgName());
//           tbTaskAttackVO.setUserName(this.tbIfadminMapper.selectById(tbTaskAttack.getJoinedTesters()).getUsername());
            tbTaskAttackVO.setJoinedTesters(this.tbIfadminMapper.selectById(tbTaskAttack.getJoinedTesters()).getUsername());
tbTaskAttackVO.setRelatedOrg(this.tbAssertOrgMapper.selectById(tbTaskAttack.getRelatedOrg()).getOrgName());
            // 设置资产名称
            tbTaskAttackVO.setTaskName(tbTaskAttack.getTaskName());
            tbTaskAttackVOList.add(tbTaskAttackVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setData(tbTaskAttackVOList);
        pageVO.setTotal(resultPage.getTotal());
        return pageVO;
    }
}
